package order;

// -*- codeing: utf-8 -*-
// @Time     :2021/12/11 17:43
// @Author   :李壮壮
//@File      :bolt1.java
//@Software  : IntelliJ IDEA


import com.alibaba.fastjson.JSONObject;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

public class bolt1 extends BaseBasicBolt {
        public void execute(Tuple input, BasicOutputCollector collector) {
            try {
                // TODO Auto-generated method stub
                byte[] bytes = input.getBinary(0);  //字节码
                //byte->String
                String line = new String(bytes);
                String[] str = line.split(",");

                String book_name = str[2];
                String book_num = str[4];
                String book_press = str[6];


                String[] bne = book_name.split(":");
                String bookname = bne[1];
                System.out.println(bookname);
                String[] bps = book_press.split(":");
                String bookpress = bps[1];
                System.out.println(bookpress);
//        String[] bnm=book_num.split(":");
//        String bookpress=bnm[1];
//        System.out.println(bookpress);

//        String bookid="{"+book_id+"}";
//        String bookname="{"+book_name+"}";
//        String booktype="{"+book_type+"}";
                String booknum = "{" + book_num + "}";
//        String bookprice="{"+book_price+"}";

//        JSONObject jsonobject2=JSONObject.parseObject(bookid);
//        String Bookid=jsonobject2.getString("book_id");
//        JSONObject jsonobject3=JSONObject.parseObject(bookname);
//        String Bookname=jsonobject3.getString("book_name");
//        JSONObject jsonobject4=JSONObject.parseObject(booktype);
//        String Booktype=jsonobject4.getString("book_type");
                JSONObject jsonobject = JSONObject.parseObject(booknum);
                String Booknum = jsonobject.getString("book_num");
                System.out.println(Booknum);

//        JSONObject jsonobject6=JSONObject.parseObject(bookprice);
//        String Bookprice=jsonobject6.getString("book_price");
//        JSONObject jsonobject7=JSONObject.parseObject(bookpress);
//        String Bookpress=jsonobject7.getString("book_press");
                //发送数据，发给需要的bolt，后面的bolt按字段索引数据
                collector.emit(new Values(bookname, Booknum,bookpress));
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {

        /**
         * 描述我将要发送数据的字段是哪些
         */
        declarer.declare(new Fields("bookname","booknum","bookpress"));
    }

}